package the.broccoli.ac.controller;

import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.ac.controller.resp.AppVersionResp;
import the.broccoli.ac.entity.App;
import the.broccoli.ac.service.IAppService;
import the.broccoli.ac.vo.AppVO;
import the.broccoli.auth.UserContext;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 应用控制器
 *
 * @author lei.liu
 * @since 2020-04-07 14:10:04
 */
@RestController
@RequestMapping("/ac/app")
public class AppController {

    @Resource
    private IAppService appService;

    @PostMapping("/add")
    public APIResult add(
            @ApiParam(name = "appName", value = "应用名称", required = true)
            @RequestParam(name = "appName", required = true) String appName,
            @ApiParam(name = "logo", value = "应用logo", required = false)
            @RequestParam(name = "logo", required = false) String logo,
            @ApiParam(name = "version", value = "版本", required = true)
            @RequestParam(name = "version") String version,
            @ApiParam(name = "descr", value = "描述", required = true)
            @RequestParam(name = "descr", required = true) String descr
    ) {
        App entity = new App();
        entity.setAppName(appName);
        entity.setLogo(logo);
        entity.setDescr(descr);
        entity.setVersion(version);
        entity.setIsDeleted(0);
        entity.setCreatedId(UserContext.userId());
        entity.setCreatedName(UserContext.userName());
        entity.setCreatedTime(new Date());
        appService.save(entity);
        return APIResult.success();
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<String> ids) {
        appService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(
            @ApiParam(name = "appId", value = "应用ID", required = true)
            @RequestParam(name = "appId", required = true) Integer appId,
            @ApiParam(name = "appName", value = "应用名称", required = true)
            @RequestParam(name = "appName", required = true) String appName,
            @ApiParam(name = "version", value = "版本", required = true)
            @RequestParam(name = "version") String version,
            @ApiParam(name = "logo", value = "应用logo", required = false)
            @RequestParam(name = "logo", required = false) String logo,
            @ApiParam(name = "descr", value = "描述", required = true)
            @RequestParam(name = "descr", required = true) String descr
    ) {
        App entity = new App();
        entity.setAppId(appId);
        entity.setAppName(appName);
        entity.setLogo(logo);
        entity.setDescr(descr);
        entity.setVersion(version);
        entity.setModifiedId(UserContext.userId());
        entity.setModifiedName(UserContext.userName());
        entity.setModifiedTime(new Date());
        appService.updateById(entity);
        return APIResult.success();
    }

    @GetMapping("/{appId}")
    public APIResult get(@PathVariable(value = "appId") String appId) {
        return APIResult.success(appService.getById(appId));
    }

    @GetMapping("/page")
    public APIResult<QueryResult> page(
            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageNum", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        AppVO vo = new AppVO();
        vo.setKeyword(keyword);
        vo.setOrderBy(orderBy);
        return APIResult.success(appService.pageList(vo, pageNum, pageSize));
    }

    @GetMapping("/tree")
    public APIResult getTree() {
        return APIResult.success(appService.getTree());
    }

    @GetMapping("/version")
    public APIResult<AppVersionResp> getVersion(@Param("appId") Integer appId) {
        AppVersionResp resp = null;
        App app = appService.getById(appId);
        if (app != null) {
            resp = new AppVersionResp();
            resp.setVersion(app.getVersion());
        }
        return APIResult.success(resp);
    }
}